package com.toweave.starter.fragments;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.toweave.starter.R;

public class HalfScreenDialogFragment extends DialogFragment {

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    // 设置Dialog无标题，这是为了可能的样式兼容问题
    setStyle(DialogFragment.STYLE_NO_TITLE, 0);
    // 使用自定义布局文件来填充Dialog
    return inflater.inflate(R.layout.fragment_half_screen_dialog, container, false);
  }

  @Override
  public void onStart() {
    super.onStart();
    Dialog dialog = getDialog();
    if (dialog != null) {
      Window window = dialog.getWindow();
      if (window != null) {
        // 设置窗口背景为透明，这可以消除边距带来的问题
        window.setBackgroundDrawable(new ColorDrawable(Color.WHITE));
        // 这里设置宽度为MATCH_PARENT，高度为屏幕的一半
        // window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, getResources().getDisplayMetrics().heightPixels / 2); // 高度为屏幕的一半
        // 调整高度为屏幕的一半
        // WindowManager.LayoutParams params = window.getAttributes();
        // params.height = getResources().getDisplayMetrics().heightPixels / 2;
        // window.setAttributes(params);
        // 设置动画、样式、位置等属性
        window.setGravity(Gravity.BOTTOM);
        window.setWindowAnimations(R.style.HalfScreenDialogAnimation); // 自定义动画
      }
    }
  }
}
